﻿using BW.ME.Exam.Infrastructure.Interfaces;
using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.ME.Exam.Infrastructure.ImPortions
{
    public class BaseRepository<T>:IBaseRepository<T> where T : class
    {
        //
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db"></param>
        public BaseRepository(SCDbContext db)
        {
            Db = db;
        }

        public SCDbContext Db { get; }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return Db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(T entity)
        {
            Db.Set<T>().Add(entity);
            return Db.SaveChanges();
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(T entity)
        {
            Db.Set<T>().Update(entity);
            return Db.SaveChanges();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public int AddRange(List<T> entities)
        {
            Db.Set<T>().AddRange(entities);
            return Db.SaveChanges();
        }
        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public int UpdateRange(List<T> entities)
        {
            Db.Set<T>().UpdateRange(entities);
            return Db.SaveChanges();
        }
    }
}
